// 引入uni-sec-check公共模块
const UniSecCheck = require('uni-sec-check');
// 引入uni-config-center公共模块
const configCenter = require('uni-config-center');
// 获取uni-id配置
const uniIdConfig = configCenter({
	pluginId: 'uni-id'
});

module.exports = {
	_before: function() {

	},
	// 测试 - 检测文本
	async textSecCheck(obj) {
		let {
			content,
			openid,
			scene,
			version = 2
		} = obj;
		// 初始化实例
		const uniSecCheck = new UniSecCheck({
			provider: 'mp-weixin',
			requestId: this.getUniCloudRequestId(), // 云函数内则写 context.requestId
		});
		// 检测文本
		const checkRes = await uniSecCheck.textSecCheck({
			content,
			openid,
			scene,
			version
		})

		if (checkRes.errCode === uniSecCheck.ErrorCode.RISK_CONTENT) {
			return {
				errCode: checkRes.errCode,
				errMsg: '文字存在风险',
				result: checkRes.result
			}
		} else if (checkRes.errCode) {
			console.log(`其他原因导致此文件未完成自动审核（错误码：${checkRes.errCode}，错误信息：${checkRes.errMsg}），需要人工审核`);
			return {
				errCode: checkRes.errCode,
				errMsg: checkRes.errMsg,
				result: checkRes.result
			}
		}
		return {
			errCode: 0,
			errMsg: ''
		};
	},
	// 测试 - 检测图片
	async imgSecCheck(obj) {
		let {
			image,
			openid,
			scene,
			version = 2
		} = obj;
		// 初始化实例
		const uniSecCheck = new UniSecCheck({
			provider: 'mp-weixin',
			requestId: this.getUniCloudRequestId(), // 云函数内则写 context.requestId
		});
		// 检测图片
		let checkRes = await uniSecCheck.imgSecCheck({
			image,
			openid,
			scene,
			version
		});

		if (checkRes.errCode === uniSecCheck.ErrorCode.RISK_CONTENT) {
			image && await uniCloud.deleteFile({
				fileList: [image]
			})
			return {
				errCode: checkRes.errCode,
				errMsg: '文件存在风险，已自动删除',
				result: checkRes.result
			}
		} else if (checkRes.errCode) {
			console.log(`其他原因导致此文件未完成自动审核（错误码：${checkRes.errCode}，错误信息：${checkRes.errMsg}），需要人工审核`);
			return {
				errCode: checkRes.errCode,
				errMsg: checkRes.errMsg,
				result: checkRes.result
			}
		}
		return {
			errCode: 0,
			errMsg: '',
			traceId: checkRes.traceId
		}
	},
	// 测试 - 检测音频
	async avSecCheck(obj) {
		let {
			mediaUrl,
			openid,
			scene,
			version = 2
		} = obj;
		// 初始化实例
		const uniSecCheck = new UniSecCheck({
			provider: 'mp-weixin',
			requestId: this.getUniCloudRequestId(), // 云函数内则写 context.requestId
		});
		// 检测音频
		let checkRes = await uniSecCheck.avSecCheck({
			mediaUrl,
			openid,
			scene,
			version
		});

		if (checkRes.errCode === uniSecCheck.ErrorCode.RISK_CONTENT) {
			mediaUrl && await uniCloud.deleteFile({
				fileList: [mediaUrl]
			})
			return {
				errCode: checkRes.errCode,
				errMsg: '文件存在风险，已自动删除',
				result: checkRes.result
			}
		} else if (checkRes.errCode) {
			console.log(`其他原因导致此文件未完成自动审核（错误码：${checkRes.errCode}，错误信息：${checkRes.errMsg}），需要人工审核`);
			return {
				errCode: checkRes.errCode,
				errMsg: checkRes.errMsg,
				result: checkRes.result
			}
		}
		return {
			errCode: 0,
			errMsg: '',
			traceId: checkRes.traceId
		}
	},
	// 测试 - 获取openid
	async getOpenid(obj) {
		let {
			code
		} = obj;
		let { appid, appsecret } = uniIdConfig.config()["mp-weixin"].oauth.weixin;
		let url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${appsecret}&js_code=${code}&grant_type=authorization_code`;
		let requestRes = await uniCloud.httpclient.request(url, {
			method: "get",
			dataType: "json",
			headers: {
				"content-type": "application/json"
			}
		})
		let result = requestRes.data;
		let res = {
			errCode: result.errcode || 0,
			errMsg: result.errmsg,
			openid: result.openid,
			appid
		};
		return res;
	},


}